<?php

namespace App\Repositories;

use App\Models\Userslog;

/**
 * @auther 商品规格属性
 */

class UserslogRepository
{

    // 列表
    public function list($where = [])
    {
        return Userslog::where($where)->orderBy('created_at', 'desc')
            ->get();
    }
    // 分页列表
    public function pageList($where = [])
    {
        !isset($where['page']) ? $page = 15 : $page = $where['page'];

        return $userList = Userslog::orderBy('created_at', 'desc')->where('state',1)->where('userid',$where['userid'])->where(function ($query) use ($where) {
                if (isset($where['type'])) {
                    $query->where('type',  $where['type']);
                }
                if (isset($where['order_sn'])) {
                    $query->where('order_sn',  $where['order_sn']);
                }
        })->paginate($page);
    }
     public function LogList($where = [])
    {
        !isset($where['page']) ? $page = 30 : $page = $where['page'];

        return $userList = Userslog::orderBy('created_at', 'desc')->where('type',1)->where('state',1)->where(function ($query) use ($where) {
            if (isset($where['start_time']) && isset($where['end_time'])) {
                    $query->whereBetween('created_at', [$where['start_time'],$where['end_time']]);      
                }
                if (isset($where['type'])) {
                    $query->where('type',  $where['type']);
                }
                if (isset($where['order_sn'])) {
                    $query->where('order_sn',  $where['order_sn']);
                }
        })->paginate($page);
    }
    // add
    public function create($param)
    {
        $sys = new Userslog();
        $sys->fill($param)->save();
        return $sys->id;
    }

    // update
    public function update($param)
    {
        $sys = Userslog::find($param['id']);
        return $sys->fill($param)->save();
    }
    public function snUpdate($param)
    {
        $sys = Userslog::find($param['order_sn']);
        return $sys->fill($param)->save();
    }

    // del
    public function delete($id)
    {
        return Userslog::where('id', $id)->delete();
    }
    public function logCount($param=[])
    {
        return Userslog::where('type',$param['type'])->where('userid',$param['userid'])->sum('price');
    }
    // info
    public function info($param)
    {
        return Userslog::where($param)->first();
    }
}
